const app = getApp();
var Keys = require('../../assets/data/keys.js')

Page({
	data: {
    Money:"",
		userInfo: {},
		hasUserInfo: false,
		canIUse: wx.canIUse('button.open-type.getUserInfo'),

		UserDetail: {
			U_Nickname: '请授权用户信息',
			U_Headimgurl: null,
			U_Age: '',
			U_Email: '',
			U_QQ: ''
		},

		TabList: [
			{
				"id": 0,
				"text": "我的订单",
				"img": "../../images/w/9.png",
				"jumpFlag": "W_OrderList",
				"isClick": false,
			},
			{
				"id": 1,
				"text": "我的地址",
				"img": "../../images/w/10.png",
				"jumpFlag": "W_AddressList",
				"isClick": false,
			},
			{
				"id": 2,
				"text": "个人信息",
				"img": "../../images/w/11.png",
				"jumpFlag": "W_PersonInfo",
				"isClick": false,
			},
			{
				"id": 3,
				"text": "专属二维码",
				"img": "../../images/w/12.png",
				"jumpFlag": "W_MyQRCode",
				"isClick": false,
			},
			{
				"id": 4,
				"text": "我的积分",
				"img": "../../images/w/13.png",
				"jumpFlag": "W_JifenTixian",
				"isClick": false,
			},
			{
				"id": 5,
				"text": "我的收藏",
				"img": "../../images/w/16.png",
				"jumpFlag": "W_Collection",
				"isClick": false,
			},
			{
				"id": 6,
				"text": "联系我们",
				"img": "../../images/w/14.png",
				"jumpFlag": "W_ContactUs",
				"isClick": false,
			},
			{
				"id": 7,
				"text": "客服中心",
				"img": "../../images/w/15.png",
				"jumpFlag": "W_CustomCenter",
        // "jumpFlag": "Fl_ceshi",

				"isClick": false,
			},
		]
	},



	/**
   * 生命周期函数--监听页面加载
   */
	onLoad: function (options) {
		//this.requestNetUserDetail();

	},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady: function () {

	},

  /**
   * 生命周期函数--监听页面显示
   */
	onShow: function () {
    var _this = this;

    wx.request({
      url: 'https://www.chnode.xyz/api/Balance.ashx?',
      data: {
        DataType: "MyBalance",
        Token: getApp().data.Token,
      },
      success: function (res) {
        console.log(res.data.data.U_Blance);
        _this.setData({
          Money: res.data.data.U_Blance,
        })
      }
    })

    this.requestNetUserDetail();
		// var _this = this;
		// if (app.globalData.userInfo) {
		// 	_this.data.UserDetail.U_Nickname = app.globalData.userInfo.nickName;
		// 	_this.data.UserDetail.U_Headimgurl = app.globalData.userInfo.avatarUrl;
		// 	_this.setData({
		// 		UserDetail: _this.data.UserDetail,
		// 		userInfo: app.globalData.userInfo,
		// 		hasUserInfo: true
		// 	})
			
		// } else if (_this.data.canIUse) {
		// 	//由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
		// 	//所以此处加入 callback 以防止这种情况
		// 	app.userInfoReadyCallback = res => {
		// 		_this.data.UserDetail.U_Nickname = app.globalData.userInfo.nickName;
		// 		_this.data.UserDetail.U_Headimgurl = app.globalData.userInfo.avatarUrl;
		// 		_this.setData({
		// 			UserDetail: _this.data.UserDetail,
		// 			userInfo: res.userInfo,
		// 			hasUserInfo: true
		// 		})
		// 		_this.requestNetUserDetail();
		// 	}
		// }
	},

  /**
   * 生命周期函数--监听页面隐藏
   */
	onHide: function () {

	},

  /**
   * 生命周期函数--监听页面卸载
   */
	onUnload: function () {

	},



	/**
	 * 点击事件--个人信息
	 */
	btnEventUserInfo: function (e) {
			wx.navigateTo({
				url: '/pages/W_PersonInfo/index?UserDetail=' + JSON.stringify(this.data.UserDetail)
      })
	},

	/**
	 * 点击事件--条目点击
	 */
	btnEventTab: function (e) {
		var index = e.currentTarget.dataset.index;
		var url_path = '/pages/' + this.data.TabList[index].jumpFlag + '/index';
		switch (index) {
			case 2:
				url_path = url_path + '?UserDetail=' + JSON.stringify(this.data.UserDetail)
				console.log(this.data.UserDetail)
				break;
		}
		// if (index == 2 && !this.data.hasUserInfo) {
		// 	wx.showModal({
		// 		title: '提示',
		// 		content: '请授权用户信息',
		// 		showCancel: false,
		// 	})
		// 	return;
		// }
		wx.navigateTo({
			url: url_path
		})
	},

	/**
	 * 网络请求--获取微信的个人信息
   * 
	 */
	// btnEventGetUserInfo: function (e) {
	// 	if (e.detail.userInfo) {
	// 		app.globalData.userInfo = e.detail.userInfo
	// 		this.requestNetUInfoInsert(
	// 			e.detail.userInfo.country,
	// 			e.detail.userInfo.province,
	// 			e.detail.userInfo.city,
	// 			e.detail.userInfo.avatarUrl,
	// 			e.detail.userInfo.nickName,
	// 			e.detail.userInfo.gender)
	// 		this.data.UserDetail.U_Nickname = app.globalData.userInfo.nickName;
	// 		this.data.UserDetail.U_Headimgurl = app.globalData.userInfo.avatarUrl;
	// 		this.setData({
	// 			UserDetail: this.data.UserDetail,
	// 			userInfo: e.detail.userInfo,
	// 			hasUserInfo: true
	// 		})
	//	}
	//},

	/**
	 * 网络请求--个人信息
	 */
	requestNetUserDetail: function () {
    console.log("bbbbbbbbb");
    console.log(getApp().data.Token);
    console.log("bbbbbbbb");
		var _this = this;
		wx.request({
			url: 'https://www.chnode.xyz/api/My.ashx?DataType=UserDetail&Token=' + getApp().data.Token,
			success: function (res) {
				if (res.data.isok == 'true') {
          console.log("mmmmmm");
          console.log(res.data);
          console.log("mmmmmm");
					_this.setData({ UserDetail: res.data.data })
				}
			},
		})
	},

	/**
	 * 网络请求--绑定用户信息
	 */
	requestNetUInfoInsert: function (param1, param2, param3, param4, param5, param6) {
		var _this = this;
		wx.request({
			url: 'https://www.chnode.xyz/api/My.ashx?DataType=UInfoInsert',
			data: {
				Openid: getApp().data.openid,
				Country: param1,
				Privince: param2,
				City: param3,
				HeadimgUrl: param4,
				NickName: param5,
				Sex: param6,
			},
			success: function (res) {
				if (res.data.isok == 'true') {
					_this.requestNetUserDetail();
				}
			}
		})
	}
})